package com.soft.javase.day11;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/5/9
 */
public class UserLogin {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入用户名: ");
        String username = scanner.next();

        System.out.print("请输入密码: ");
        String password = scanner.next();

        boolean isLogin = login(username, password);

        if (isLogin) {
            System.out.println("登录成功！欢迎，" + username + "！");
        } else {
            System.out.println("登录失败！用户名或密码错误。");
        }

    }

    private static boolean login(String username, String password) {
        try (BufferedReader reader = new BufferedReader(new FileReader("emp.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("#");
                if (parts.length == 2) {
                    String storeUsername = parts[0].trim();
                    String storePassword = parts[1].trim();

                    if (storeUsername.equals(username) && storePassword.equals(password)) {
                        return true;
                    }
                }
            }
        } catch (IOException e) {
            System.err.println("读取用户数据文件时出错: " + e.getMessage());
        }
        return false;
    }
}
